﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;

public class GDTEventAgent : MonoBehaviour
{
    public enum EMActionName
    {
        START_APP,//   应用启动
        PAGE_VIEW,//   页面浏览
        REGISTER,//    注册
        VIEW_CONTENT,//    内容浏览
        CONSULT,// 咨询
        ADD_TO_CART,// 加入购物车
        PURCHASE,//    购买
        SEARCH,//  搜索
        ADD_TO_WISHLIST,// 加入收藏
        INITIATE_CHECKOUT,//   开始结算
        COMPLETE_ORDER,//  下单
        DOWNLOAD_APP,//    下载应用
        RATE,//    评分
        RESERVATION,// 预订
        SHARE,//   分享
        APPLY,//   申请（用于金融广告主的申请贷款、开卡等）
        CLAIM_OFFER,// 领取卡券
        NAVIGATE,//    导航
        PRODUCT_RECOMMEND,//   商品推荐
    }
#if UNITY_IPHONE
    /// <summary>
    /// 初始化
    /// </summary>
    /// <param name="actionSetID"></param>
    /// <param name="strSecretKey"></param>
    [DllImport("__Internal")]
    static extern void InitGDTEvent(string actionSetID, string strSecretKey);
    /// <summary>
    /// 通用上报
    /// </summary>
    /// <param name="strActionName">事件名</param>
    /// <param name="actionParam">事件值[key,value]</param>
    [DllImport("__Internal")]
    static extern void LogAction(string strActionName, string strParam);
    /// <summary>
    ///   @param method 表示注册方式，业务方可以传任意可标识注册方式的值，如注册方式为手机号：method = @"phone" 微信注册：method = @“WeChat” 邮箱：method = @"mail"等。这个参数作用：方便业务方在数据平台以method为key查询数据
    /// @param isSuccess 是否注册成功
    /// </summary>
    [DllImport("__Internal")]
    static extern void reportRegisterActionWithMethod(string strMethod, bool isSuccess);
    /// <summary>
    /// @param method 表示登录的方式，如游戏账号、手机号等
    /// @param isSuccess 是否登录成功
    /// </summary>
    [DllImport("__Internal")]
    static extern void reportLoginActionWithMethod(string strMethod, bool isSuccess);
    /// <summary>
    ///@param type 社交账号类型 如如微信、微博等
    ///@param isSuccess 是否绑定成功
    /// </summary>
    [DllImport("__Internal")]
    static extern void reportBindSocialAccountActionWithType(string strType, bool isSuccess);
    /// <summary>
    // @param questID 教学/任务/副本等关卡标识符
    // @param type 节点类型
    //@param name 教学/任务/副本等关卡名称
    //@param number 第几个任务节点
    //@param desc 节点描述
    //@param isSuccess 节点是否完成
    /// </summary>
    [DllImport("__Internal")]
    static extern void reportFinishQuestActionWithQuestID(string strQuestID, string strQuestType, string strQuestName, int iQuestNumber, string strDesc, bool isSuccess);
    /// <summary>
    /// @param role 游戏角色
    /// </summary>
    [DllImport("__Internal")]
    static extern void reportCreateRoleActionWithRole(string strRole);
    /// <summary>
    /// @param level 当前用户等级
    /// </summary>
    [DllImport("__Internal")]
    static extern void reportUpgradeLevelActionWithLevel(int iLevel);
    /// <summary>
    //@param type 内容类型如“配备”、“皮肤”
    //@param name 商品或内容名称
    //@param contentID 商品或内容标识符
    /// </summary>
    [DllImport("__Internal")]
    static extern void reportViewContentActionWithContentType(string strType, string strName, string strContentID);
    /// <summary>

    //@param type 内容类型如“配备”、“皮肤”
    //@param name 商品或内容名称
    //@param contentID 商品或内容标识符
    //@param number 商品数量
    //@param isSuccess 加入购买/购物车是否成功
    /// </summary>
    [DllImport("__Internal")]
    static extern void reportAddingToCartActionWithContentType(string strType, string strName, string strContentID, int iNumber, bool isSuccess);
    /// <summary>
    //@param type 内容类型如“配备”、“皮肤"
    //@param name 商品或内容名称
    //@param contentID 商品或内容标识符
    //@param number 商品数量
    //@param isVirtualCurrency 是否使用的是虚拟货币
    //@param virtualCurrencyType 虚拟货币类型，如"元宝"、“金币”等
    //@param realCurrencyType 真实货币类型，ISO 4217代码，如：“USD”
    //@param isSuccess 提交购买/下单是否成功
    /// </summary>
    [DllImport("__Internal")]
    static extern void reportCheckoutActionWithContentType(string strType
        , string strName
        , string strContentID
        , int iNumber
        , bool isVirtualCurrency
        , string strVirtualCurrencyType
        , string strrealCurrencyType
        , bool isSuccess);

    /// <summary>
    //@param type 内容类型如“配备”、“皮肤”
    //@param name 商品或内容名称
    //@param contentID 商品或内容标识符
    //@param number 商品数量
    //@param channel 支付渠道名，如支付宝、微信等
    //@param realCurrency 真实货币类型，ISO 4217代码，如：“USD”
    //@param amount 本次支付的真实货币的金额
    //@param isSuccess 支付是否成功
    /// </summary>
    [DllImport("__Internal")]
    static extern void reportPurchaseActionWithContentType(string strType
        , string strName
        , string strContentID
        , int iNumber
        , string strChannel
        , string strRealCurrencyType
        , int iAmount
        , bool isSuccess);
    /// <summary>
    ///   @param channel 支付渠道名，如支付宝、微信支付等
    ///@param isSuccess 添加支付渠道是否成功
    /// </summary>
    [DllImport("__Internal")]
    static extern void reportAddPaymentChannelActionWithChannel(string strChannel, bool isSuccess);
    /// <summary>
    /// @param rate 评分
    /// </summary>
    [DllImport("__Internal")]
    static extern void reportRateActionWithRate(float rate);
    /// <summary>
    /// 
    /// </summary>
    [DllImport("__Internal")]
    static extern void reportShareActionWithChannel(string strChannel, bool isSuccess);
#endif

    public static GDTEventAgent Instance
    {
        get; private set;
    }

    [RuntimeInitializeOnLoadMethod]
    private static void Init()
    {
        GameObject obj = new GameObject("GDTEventAgent");
        Instance = obj.AddComponent<GDTEventAgent>();
        DontDestroyOnLoad(obj);
    }

    /// <summary>
    /// 初始化
    /// </summary>
    /// <param name="actionSetID">事件跟踪</param>
    /// <param name="strSecretKey">API密钥</param>
    public void AgentInitGDTEvent(string actionSetID, string strSecretKey, bool isDebug)
    {
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
#if UNITY_IPHONE
            InitGDTEvent(actionSetID, strSecretKey);
#endif
            if (isDebug)
            {
                var dicThem = new Dictionary<string, string>();
                dicThem.Add("testK", "testV");
                AgentLogAction(EMActionName.ADD_TO_CART, dicThem);
                AgentreportAddingToCartActionWithContentType("testType", "testName", "testContentID", 0, true);
                AgentreportAddPaymentChannelActionWithChannel("testChanel", true);
                AgentreportBindSocialAccountActionWithType("testType", true);
                AgentreportCheckoutActionWithContentType("testType", "testName", "testContentID", 0, true, "testVirtual", "testReal", true);
                AgentreportCreateRoleActionWithRole("testRole");
                AgentreportFinishQuestActionWithQuestID("testQuestID", "testQuestType", "testQuestName", 0, "testDesc", true);
                AgentreportLoginActionWithMethod("testMethod", true);
                AgentreportPurchaseActionWithContentType("testType", "testName", "strContentID", 0, "strChannel", "strTestReal", 0, true);
                AgentreportRateActionWithRate(1);
                AgentreportRegisterActionWithMethod("testMethod", true);
                AgentreportShareActionWithChannel("testChannel", true);
                AgentreportUpgradeLevelActionWithLevel(100);
                AgentreportViewContentActionWithContentType("strTestType", "strTestName", "strContentID");
            }
        }
    }

    /// <summary>
    /// 尽量采用枚举接口
    /// </summary>
    /// <param name="strActionName">事件名</param>
    /// <param name="actionParam">事件值[key,value]</param>
    public void AgentLogAction(string strActionName, Dictionary<string, string> actionParam)
    {
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            string strThem = string.Empty;
            int iThem = 0;
            foreach (var it in actionParam)
            {
                strThem += (it.Key + ":" + it.Value);
                if (iThem + 1 < actionParam.Count)
                    strThem += ",";
                iThem++;
            }
#if UNITY_IPHONE
            LogAction(strActionName, strThem);
#endif
        }
    }

    /// <summary>
    /// 通用上报
    /// </summary>
    /// <param name="strActionName">事件名</param>
    /// <param name="actionParam">事件值[key,value]</param>
    public void AgentLogAction(EMActionName actionName, Dictionary<string, string> actionParam)
    {
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            AgentLogAction(actionName.ToString(), actionParam);
        }
    }

    /// <summary>
    ///   @param method 表示注册方式，业务方可以传任意可标识注册方式的值，如注册方式为手机号：method = @"phone" 微信注册：method = @“WeChat” 邮箱：method = @"mail"等。这个参数作用：方便业务方在数据平台以method为key查询数据
    /// @param isSuccess 是否注册成功
    /// </summary>
    public void AgentreportRegisterActionWithMethod(string strMethod, bool isSuccess)
    {
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
#if UNITY_IPHONE
            reportRegisterActionWithMethod(strMethod, isSuccess);
#endif
        }
    }
    /// <summary>
    /// @param method 表示登录的方式，如游戏账号、手机号等
    /// @param isSuccess 是否登录成功
    /// </summary>
    public void AgentreportLoginActionWithMethod(string strMethod, bool isSuccess)
    {
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
#if UNITY_IPHONE
            reportLoginActionWithMethod(strMethod, isSuccess);
#endif
        }
    }

    /// <summary>
    ///@param type 社交账号类型 如如微信、微博等
    ///@param isSuccess 是否绑定成功
    /// </summary>
    public void AgentreportBindSocialAccountActionWithType(string strType, bool isSuccess)
    {
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
#if UNITY_IPHONE
            reportBindSocialAccountActionWithType(strType, isSuccess);
#endif
        }
    }
    /// <summary>
    // @param questID 教学/任务/副本等关卡标识符
    // @param type 节点类型
    //@param name 教学/任务/副本等关卡名称
    //@param number 第几个任务节点
    //@param desc 节点描述
    //@param isSuccess 节点是否完成
    /// </summary>
    public void AgentreportFinishQuestActionWithQuestID(string strQuestID
        , string strQuestType
        , string strQuestName
        , int iQuestNumber
        , string strDesc
        , bool isSuccess)
    {
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
#if UNITY_IPHONE
            reportFinishQuestActionWithQuestID(strQuestID
            , strQuestType
            , strQuestName
            , iQuestNumber
            , strDesc
            , isSuccess);
#endif
        }
    }

    /// <summary>
    /// @param role 游戏角色
    /// </summary>
    public void AgentreportCreateRoleActionWithRole(string strRole)
    {
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
#if UNITY_IPHONE
            reportCreateRoleActionWithRole(strRole);
#endif
        }
    }
    /// <summary>
    /// @param level 当前用户等级
    /// </summary>
    public void AgentreportUpgradeLevelActionWithLevel(int iLevel)
    {
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
#if UNITY_IPHONE
            reportUpgradeLevelActionWithLevel(iLevel);
#endif
        }
    }

    /// <summary>
    //@param type 内容类型如“配备”、“皮肤”
    //@param name 商品或内容名称
    //@param contentID 商品或内容标识符
    /// </summary>
    public void AgentreportViewContentActionWithContentType(string strType
        , string strName, string strContentID)
    {
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
#if UNITY_IPHONE
            reportViewContentActionWithContentType(strType, strName, strContentID);
#endif
        }
    }

    /// <summary>
    //@param type 内容类型如“配备”、“皮肤”
    //@param name 商品或内容名称
    //@param contentID 商品或内容标识符
    //@param number 商品数量
    //@param isSuccess 加入购买/购物车是否成功
    /// </summary>
    public void AgentreportAddingToCartActionWithContentType(string strType
        , string strName
        , string strContentID
        , int iNumber
        , bool isSuccess)
    {
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
#if UNITY_IPHONE
            reportAddingToCartActionWithContentType(strType
            , strName, strContentID, iNumber, isSuccess);
#endif
        }
    }

    /// <summary>
    //@param type 内容类型如“配备”、“皮肤"
    //@param name 商品或内容名称
    //@param contentID 商品或内容标识符
    //@param number 商品数量
    //@param isVirtualCurrency 是否使用的是虚拟货币
    //@param virtualCurrencyType 虚拟货币类型，如"元宝"、“金币”等
    //@param realCurrencyType 真实货币类型，ISO 4217代码，如：“USD”
    //@param isSuccess 提交购买/下单是否成功
    /// </summary>
    public void AgentreportCheckoutActionWithContentType(string strType
        , string strName
        , string strContentID
        , int iNumber
        , bool isVirtualCurrency
        , string strVirtualCurrencyType
        , string strrealCurrencyType
        , bool isSuccess)
    {
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
#if UNITY_IPHONE
            reportCheckoutActionWithContentType(strType
            , strName
            , strContentID
            , iNumber
            , isVirtualCurrency
            , strVirtualCurrencyType
            , strrealCurrencyType
            , isSuccess);
#endif
        }
    }

    /// <summary>
    //@param type 内容类型如“配备”、“皮肤”
    //@param name 商品或内容名称
    //@param contentID 商品或内容标识符
    //@param number 商品数量
    //@param channel 支付渠道名，如支付宝、微信等
    //@param realCurrency 真实货币类型，ISO 4217代码，如：“USD”
    //@param amount 本次支付的真实货币的金额
    //@param isSuccess 支付是否成功
    /// </summary>
    public void AgentreportPurchaseActionWithContentType(string strType
        , string strName
        , string strContentID
        , int iNumber
        , string strChannel
        , string strRealCurrencyType
        , int iAmount
        , bool isSuccess)
    {
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
#if UNITY_IPHONE
            reportPurchaseActionWithContentType(strType
            , strName
            , strContentID
            , iNumber
            , strChannel
            , strRealCurrencyType
            , iAmount
            , isSuccess);
#endif
        }
    }

    /// <summary>
    ///   @param channel 支付渠道名，如支付宝、微信支付等
    ///@param isSuccess 添加支付渠道是否成功
    /// </summary>
    public void AgentreportAddPaymentChannelActionWithChannel(string strChannel, bool isSuccess)
    {
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
#if UNITY_IPHONE
            reportAddPaymentChannelActionWithChannel(strChannel, isSuccess);
#endif
        }
    }
    public void AgentreportRateActionWithRate(float rate)
    {
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
#if UNITY_IPHONE
            reportRateActionWithRate(rate);
#endif
        }
    }
    public void AgentreportShareActionWithChannel(string strChannel, bool isSuccess)
    {
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
#if UNITY_IPHONE
            reportShareActionWithChannel(strChannel, isSuccess);
#endif
        }
    }
}
